package com.black.cat.jvm.instructions.constants.ldc;


import com.black.cat.jvm.instructions.base.InstructionIndex8;
import com.black.cat.jvm.rtda.Frame;
import com.black.cat.jvm.rtda.JObject;
import com.black.cat.jvm.rtda.OperandStack;
import com.black.cat.jvm.rtda.heap.constantpool.ClassRef;
import com.black.cat.jvm.rtda.heap.constantpool.RunTimeConstantPool;
import com.black.cat.jvm.rtda.heap.methodarea.JClass;
import com.black.cat.jvm.rtda.heap.methodarea.StringPool;


public class LDC extends InstructionIndex8 {

    @Override
    public void execute(Frame frame) {
        _ldc(frame, this.idx);
    }

    private void _ldc(Frame frame, int idx) {
        OperandStack stack = frame.operandStack();
        JClass clazz = frame.method().clazz();
        RunTimeConstantPool runTimeConstantPool = frame.method().clazz().constantPool();
        Object c = runTimeConstantPool.getConstants(idx);

        if (c instanceof Integer) {
            stack.pushInt((Integer) c);
            return;
        }

        if (c instanceof Float) {
            stack.pushFloat((Float) c);
            return;
        }

        if (c instanceof String) {
            JObject internedStr = StringPool.jString(clazz.loader, (String) c);
            stack.pushRef(internedStr);
            return;
        }
//
//        if (c instanceof ClassRef) {
//            ClassRef classRef = (ClassRef) c;
//            JObject classObj = classRef.resolvedClass().jClass();
//            stack.pushRef(classObj);
//            return;
//        }

        System.out.println("todo ldc " + c.getClass().getName());
    }

}
